//
//  HZImageCollView.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/22.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit




class HZImageCollView: UICollectionView{
    
 

    
    
    //定义一个元祖类型进行设置
    var dataInfo:(pic_urls:[HZImageModel],size:CGSize)? {
        
        
        didSet{
            
            
            //判断是单图尺寸还是多图的
            if dataInfo?.pic_urls.count == 1{
                
                //设置每个 item 的大小
                let flow = self.collectionViewLayout as! UICollectionViewFlowLayout
                
//                print(dataInfo?.size ?? CGSizeZero)
                flow.itemSize = dataInfo?.size ?? CGSizeZero

                
                
            }else {
                
                
                //设置每个 item 的大小
                let flow = self.collectionViewLayout as! UICollectionViewFlowLayout
                
                flow.itemSize = CGSize(width: itemW, height: itemW)

                
            }
            
            
            
            
            
            
            //更新 colletionView的大小
            self.snp_updateConstraints(closure: { (make) -> Void in
                
                
                make.size.equalTo(dataInfo?.size ?? CGSizeZero)
            })
            
            
            //赋值完立马刷新数据
            reloadData()
            
        }
        
        
        
    }
    
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: layout)
        
        
      
        setupUI()
    }
    
    
    private func setupUI(){
        
        //设置背景色
        backgroundColor = UIColor.whiteColor()
        
        
        
        //设置每个 item 的大小
        let flow = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        flow.itemSize = CGSize(width: itemW, height: itemW)
        
        flow.minimumInteritemSpacing = CGFloat(HZItemMagin)
        flow.minimumLineSpacing = CGFloat(HZItemMagin)
        
        
        //设置代理
        dataSource = self
        delegate = self
        
        //注册 item
        registerClass(HZImgItem.self, forCellWithReuseIdentifier: "item")
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

    

}


extension HZImageCollView:SDPhotoBrowserDelegate {
    
    
    //返回原来的小图
    func photoBrowser(browser: SDPhotoBrowser!, placeholderImageForIndex index: Int) -> UIImage! {
        
        let cell = cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! HZImgItem
        
        return cell.imageView.image
        
    }
    
    
    //返回高清的大图
    func photoBrowser(browser: SDPhotoBrowser!, highQualityImageURLForIndex index: Int) -> NSURL! {
        
        //把小图的 url 改成高清大图的 url
        let urlStirng =  dataInfo!.pic_urls[index].thumbnail_pic! .stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
        
        let url = NSURL(string: urlStirng)
        


        
        return url!
    }
    
    
}



// MARK: -  数据源代理的分类
extension HZImageCollView:UICollectionViewDataSource,UICollectionViewDelegate {
    
    
    // MARK: -  选中某个item 的时候调用
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
   
        //创建图片游览器
        let browser = SDPhotoBrowser()
        
        //设置父View
        browser.sourceImagesContainerView = self
        
        //原图的总数量
        browser.imageCount = dataInfo!.pic_urls.count
        
        //当前要展示的图片的索引
        browser.currentImageIndex = indexPath.item
        
        //设置代理
        browser.delegate = self
        
        //展示图片游览器
        browser.show()
        
    }
    
    
    
    
     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataInfo?.pic_urls.count ?? 0
    }
    
     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let item = collectionView.dequeueReusableCellWithReuseIdentifier("item", forIndexPath: indexPath) as! HZImgItem
        
        item.imgUrl = dataInfo!.pic_urls[indexPath.item]
        
        
        return item
    }
    
    
    
}
